package com.taru.order.controller;


import com.taru.order.common.JsonResult;
import com.taru.order.entity.Articl;
import com.taru.order.entity.ArticlLikes;
import com.taru.order.entity.Colletion;
import com.taru.order.service.ArtServiceImpl;
import com.taru.order.util.Util;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class ArticlController {

    ArtServiceImpl artService=new ArtServiceImpl();

    /**
     * 添加文章
     */
    @RequestMapping(value = "/articls",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult insert(@RequestBody Articl articl){
        JsonResult result=null;
        articl.setCode(Util.getUUID());
        try{
            int code=artService.addArticl(articl);
            if(code>0){
                result =new JsonResult(200,"添加成功");
            }else {
                result=new JsonResult(404,"添加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }


    /**
     * 根据articl_id获取获取文章详情
     */
    @RequestMapping(value = "/articls/{id}",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult getArticlById(@PathVariable("id") String id){
        JsonResult result=null;
        try{
            Articl articl=artService.getArticlById(id);
            if(articl !=null){
                result =new JsonResult(200,"获取成功",articl);
            }else {
                result=new JsonResult(404,"获取失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 修改文章（只修改内容）
     */
    @RequestMapping(value = "/articls",method = RequestMethod.PUT)
    @ResponseBody
    public JsonResult modArticl(@RequestBody Articl articl){
        JsonResult result=null;
        try{
            int code=artService.modArticl(articl);
            if(code>0){
                result =new JsonResult(200,"修改成功");
            }else {
                result=new JsonResult(404,"修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 修改文章状态
     */
    @RequestMapping(value = "/articls/status",method = RequestMethod.PATCH)
    @ResponseBody
    public JsonResult modArticlStatus(@RequestBody Articl articl){
        JsonResult result=null;
        try{
            int code=artService.modArticlStatus(articl);
            if(code>0){
                result =new JsonResult(200,"修改成功");
            }else {
                result=new JsonResult(404,"修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 批量删除文章（顺带着删除数据库中的图片路劲）
     */
    @RequestMapping(value = "/articls",method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResult removeArticls(@RequestBody() List<Integer> list){
        JsonResult result=null;
        try{
            int code=artService.removeArticls(list);
            if(code>0){
                result =new JsonResult(200,"删除成功");
            }else {
                result=new JsonResult(404,"删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 修改文章的logo（删除原有路径，添加新的路径）
     */
    @RequestMapping(value = "/articls/logo",method = RequestMethod.PATCH)
    @ResponseBody
    public JsonResult modArticlLogo(@RequestBody Articl articl){
        JsonResult result=null;
        try{
            int code=artService.modArticlLogo(articl);
            if(code>0){
                result =new JsonResult(200,"修改成功");
            }else {
                result=new JsonResult(404,"修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 增加点赞
     */
        @RequestMapping(value = "/articl/inLikes",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult inArticlLikes( @RequestBody  ArticlLikes likes){
        JsonResult result=null;
        try{
            int code=artService.inArticlLikes(likes);
            if(code>0){
                result =new JsonResult(200,"增加成功");
            }else {
                result=new JsonResult(404,"增加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 减少点赞
     */
    @RequestMapping(value = "/articl/outLikes",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult outArticlLikes(@RequestBody ArticlLikes likes){
        JsonResult result=null;
        try{
            int code=artService.outArticlLikes(likes);
            if(code>0){
                result =new JsonResult(200,"减少成功");
            }else {
                result=new JsonResult(404,"点赞量已为0");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 判断有没有被点过赞
     */
    @RequestMapping(value = "/articl/likes/judge",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult queryArticlLikes(@RequestBody ArticlLikes likes){
        JsonResult result=null;
        try{
            boolean isExist=artService.queryArticlLikes(likes);
            result =new JsonResult(200,"查询成功",isExist);
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 给文章添加专题
     */
    @RequestMapping(value = "/articl/addArtGroup",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult addArtGroup(@RequestBody Articl articl){
        JsonResult result=null;
        try{
            int code=artService.addArtGroup(articl);
            if(code>0){
                result =new JsonResult(200,"添加文章专题成功");
            }else {
                result=new JsonResult(404,"添加文章专题失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 按照时间查询文章列表
     */
    @RequestMapping(value = "/articl/time",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult getArticlByYearMonth(String year,String month){
        JsonResult result=null;
        try{
            List list=artService.getArticlByYearMonth(year,month);
            if(list !=null){
                result =new JsonResult(200,"获取成功",list);
            }else {
                result=new JsonResult(404,"获取失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 查询正在连载的文章
     */
    @RequestMapping(value = "/articl/noEnd",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult getArticlNoEnd(){
        JsonResult result=null;
        try{
            List list=artService.getArticlNoEnd();
            if(list !=null){
                result =new JsonResult(200,"获取成功",list);
            }else {
                result=new JsonResult(404,"获取失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 查询已完结的文章
     */
    @RequestMapping(value = "/articl/end",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult getArticlEnd(){
        JsonResult result=null;
        try{
            List list=artService.getArticlEnd();
            if(list !=null){
                result =new JsonResult(200,"获取成功",list);
            }else {
                result=new JsonResult(404,"获取失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 判断文章有没有被收藏
     */
    @RequestMapping(value = "/articl/isCollection",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult queryArticlCollection(@RequestBody Colletion colletion){
        JsonResult result=null;
        try{
            boolean isExist=artService.queryArticlCollection(colletion);
            result =new JsonResult(200,"获取成功",isExist);
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
}
